#!/bin/sh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# c.hsc src/hsc/scripts/formatdvd.sh 1.1 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2001 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
#!/bin/sh
#---------------------------------------------------------------------------------
# formatMedia.sh
# 
# Usage: formatMedia <type>, where type is 0=DVD-RAM, 1=diskette
#
# Return Codes:
# 1 - Error locating log directory
# 2 - unknwon DVD format error
# 3 - Media inserted improperly
# 4 - Media is write-protected
# 5 - unknown floppy format error

#---------------------------------------------------------------------------------
# directory and filename which records the format actions.
#---------------------------------------------------------------------------------
LOGDIR=/var/hsc/log
LOG=$LOGDIR/formatMedia.log

LOG_ERROR_LOG=/tmp/formatMedia.log

# STDOUT directed to /tmp/format_output + PID
FORMAT_OUTPUT=/tmp/format_output$$


# common exit point for script
exit_cleanup() {
    rm -f $FORMAT_OUTPUT
    exit $1
}  


#-------------------------------------------------------------------------------
# the fully qualified format commands
#-------------------------------------------------------------------------------
FORMAT_DVD_CMD=/bin/mkudf
FORMAT_DISKETTE_CMD=/sbin/mkdosfs



#-------------------------------------------------------------------------------
# Check if the directory for the log file exists.
#-------------------------------------------------------------------------------
if [ ! -d $LOGDIR ] ; then
   echo "=================================================================" > $LOG_ERROR_LOG
   
   #----------------------------------------------------------------------------
   # Ensure permissions are set so other users can overwrite this log file
   #----------------------------------------------------------------------------
   chmod 666 $LOG_ERROR_LOG
   
   echo -e "Format media log for `date`." >> $LOG_ERROR_LOG
   echo "Format media directory, <$LOGDIR>, does not exist. Program exiting" >> $LOG_ERROR_LOG
   exit_cleanup 1
fi


#-------------------------------------------------------------------------------
# Start a new log to record the format actions.
#-------------------------------------------------------------------------------
echo "=================================================================" > $LOG

#-------------------------------------------------------------------------------
# Ensure permissions are set so other users can overwrite this log file
#-------------------------------------------------------------------------------
chmod 666 $LOG

echo -e "Format Media log for `date`." >> $LOG
echo >> $LOG
echo "Input parameter is: $1" >> $LOG

#-------------------------------------------------------------------------------
# the target output media - 0=DVD-RAM, 1=floppy
#-------------------------------------------------------------------------------
MEDIA_TYPE=$1

# Set the output media mount point variable
if test "$MEDIA_TYPE" = "0" ; then
         
         #---------------------------------------------------------------------------------
         # device name: the device name in the /dev directory
         # DVD-RAM drive known to be installed as device /dev/hdc - 4/25/01 SLF
         #---------------------------------------------------------------------------------
         DEVICENAME=/dev/hdc
         
         # Format the DVD using mkudf command
         echo "Formating DVD catridge..." >> $LOG
         
         $FORMAT_DVD_CMD --media-type=dvdram --blocksize=2048 --partition-type=normal --volume-number=0 $DEVICENAME > $FORMAT_OUTPUT 2>&1
         if [ $? -ne 0 ]; then
                  if grep "No medium found" $FORMAT_OUTPUT; then
                  echo "The DVD was not inserted properly in the drive" >> $LOG
                  exit_cleanup 3
                  fi
                  
                  echo "Unknown error while formatting DVD:" >> $LOG
                  cat $FORMAT_OUTPUT >> $LOG
                  exit_cleanup 2
         else
                  echo "DVD-RAM format completed successfully." >> $LOG
                  exit_cleanup 0
         fi	
else
         
         #---------------------------------------------------------------------------------
         # device name: the device name in the /dev directory
         # floppy drive known to be installed as device /dev/fd0
         #---------------------------------------------------------------------------------
         DEVICENAME=/dev/fd0

         # unmount the media in case someone left a floppy mounted via an 'xterm' session.
         # It should not be a problem if we get an error here - matter of fact, we expect it.
         # This is quicker that checking to see if it's already mounted, then deciding
         # whether or not to unmount it. Defect 363071
         umount /mnt/floppy                  

         # Format the diskette using mkdosfs command
         $FORMAT_DISKETTE_CMD $DEVICENAME > $FORMAT_OUTPUT 2>&1
         if [ $? -ne 0 ]; then
                  if grep "No such device" $FORMAT_OUTPUT; then
                  echo "The diskette was not inserted properly in the drive" >> $LOG
                  exit_cleanup 3
                  fi
                  
                  if grep "unable to open" $FORMAT_OUTPUT; then
                  echo "The diskette is write protected" >> $LOG
                  exit_cleanup 4
                  fi
                  
                  echo "Unknown error while formatting diskette:" >> $LOG
                  cat $FORMAT_OUTPUT >> $LOG
                  exit_cleanup 5
         else
                  echo "floppy diskette format completed successfully." >> $LOG
                  exit_cleanup 0
         fi
fi
